layui.define(['jquery'],function(exports){
    let $ = layui.$;
    let addressMap={};
    let AddressSelect = function (divid,_form,value) {
        var $this=this;
        $this.$id=divid;
        $this.level0id=this.$id+"_level0";
        $this.level1id=this.$id+"_level1";
        $this.level2id=this.$id+"_level2";
        $this.level3id=this.$id+"_level3";
        $this._form=_form;
        addressMap[divid]=$this;
        _form.on('select',function(data){
            var $id = $(data.elem).attr('id');
            var $value = data.value;
            var nextId = undefined;
            var clear=[];
            let $that=addressMap[$id.slice(0,$id.length-7)];
            if($id==$that.level0id){
                nextId=$that.level1id;
                clear.push($that.level2id);
                clear.push($that.level3id);
            }
            if($id==$that.level1id){
                nextId=$that.level2id;
                clear.push($that.level3id);
            }
            if($id==$that.level2id){
                nextId=$that.level3id;
            }

            if(clear.length>0){
                for (let i = 0; i < clear.length; i++) {
                    $('#'+clear[i]).children('option:not([default])').remove();
                }
            }
            if(nextId){
                renderSelect(nextId,$value);
            }
        });

        let renderSelect = function(id,parentCode,selectVal){
            $.ajax({
                type: 'post',
                url: Feng.ctxPath + "/zharea/list",
                contentType: "application/json",
                dataType: "json",
                async: false,
                data: JSON.stringify({'parentCode':parentCode}),
                beforeSend: function (data) {
                },
                success: function (data) {
                    $('#'+id).children('option:not([default])').remove();
                    var selected = false;
                    for (let i = 0; i < data.data.length; i++) {
                        var value = data.data[i];
                        var optionHtml=[];
                        optionHtml.push("<option value='"+value.areaCode+"'");
                        if(selectVal && selectVal==value.areaCode){
                            optionHtml.push(" selected='true' ");
                            selected=value.areaCode;
                        } else {
                            if(value.areaCode==='120112000000' ||
                                value.areaCode==='120100000000' ||
                                value.areaCode==='120000000000'
                            ){
                                optionHtml.push(" selected='true' ");
                                selected=value.areaCode;
                            }
                        }
                        optionHtml.push(">");
                        optionHtml.push(value.name);
                        optionHtml.push("</option>");
                        $('#'+id).append(optionHtml.join(""));
                    }
                    let $that = addressMap[id.slice(0, id.length - 7)];
                    $that._form.render('select');
                    $('#'+id).siblings("div.layui-form-select").find('dl dd[lay-value=' +selected + ']').click();
                },
                error: function (xhr) {
                    Feng.error("获取省市失败！" + xhr.responseJSON.message);
                }
            });

        }

        if(typeof value =="undefined" || value == null|| value.length < 12) {
            renderSelect(this.level0id,0);
        }else {
            $.ajax({
                type: 'get',
                url: Feng.ctxPath + "/zharea/detail?areaCode="+value,
                contentType: "application/json",
                async: false,
                success: function (data) {
                    var values=[0];
                    values = values.concat(data.data.mergerName.split(','));
                    console.info(values);
                    renderSelect($this.level0id,values[0],values[1]);
                    renderSelect($this.level1id,values[1],values[2]);
                    renderSelect($this.level2id,values[2],values[3]);
                    renderSelect($this.level3id,values[3],values[4]);
                },
                error: function (xhr) {
                    Feng.error("获取省市失败！" + xhr.responseJSON.message);
                }
            });
        }



    };

    AddressSelect.prototype={

    }




    exports('address', AddressSelect);
});